library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.std_logic_ARITH.all;
use IEEE.std_logic_unsigned.all;


entity vga_ctrl is
    Port (
			   mclk : in std_logic;
         red_in : in std_logic;
         grn_in : in std_logic;
         blu_in : in std_logic;
         hs : out std_logic;
         vs : out std_logic;
         red_out : out std_logic;
         grn_out : out std_logic;
         blu_out : out std_logic;
			pixel_row: out std_logic_vector(9 downto 0);
			pixel_col: out std_logic_vector(9 downto 0)
			);

			attribute loc: string;
			
			attribute loc of mclk: signal is "T9";
			attribute loc of red_in: signal is "K13";
			attribute loc of grn_in: signal is "K14";
			attribute loc of blu_in: signal is "J13";
			attribute loc of hs: signal is "R9";
			attribute loc of vs: signal is "T10";
			attribute loc of red_out: signal is "R12";
			attribute loc of grn_out: signal is "T12";
			attribute loc of blu_out: signal is "R11";

end vga_ctrl;

architecture Behavioral of vga_ctrl is


	constant hpixels		: std_logic_vector(9 downto 0) := "1100100000";	 -- Value of pixels in a horizontal line (800)
	constant vlines		: std_logic_vector(9 downto 0) := "1000001001";	 --Number of horizontal lines in the display (521)
	
	constant hbp			: std_logic_vector(9 downto 0) := "0010010000";	 --Horizontal back porch (144)
	constant hfp			: std_logic_vector(9 downto 0) := "1100010000";	 --Horizontal front porch (784)
	constant	vbp			: std_logic_vector(9 downto 0) := "0000011111";	 --Vertical back porch (31)
	constant vfp			: std_logic_vector(9	downto 0) := "0111111111";	 --Vertical front porch (511)
	
	signal hc, vc			: std_logic_vector(9 downto 0);						 --These are the Horizontal and Vertical counters
	signal clkdiv			: std_logic;												 --Clock divider
	signal vidon			: std_logic;												 --Tells whether or not its ok to display data
	signal vsenable		: std_logic;												 --Enable for the Vertical counter
	
	signal hs_aux, vs_aux: std_logic;

begin
	--This cuts the 50Mhz clock in half
	process(mclk)
		begin
			if(mclk = '1' and mclk'EVENT) then
				clkdiv <= not clkdiv;
			end if;
		end process;																			

	--Runs the horizontal counter
	process(clkdiv)
		begin
			if(clkdiv = '1' and clkdiv'EVENT) then
				if hc = hpixels then														 --If the counter has reached the end of pixel count
					hc <= "0000000000";													 --reset the counter
					vsenable <= '1';														 --Enable the vertical counter to increment
				else
					hc <= hc + 1;															 --Increment the horizontal counter
					vsenable <= '0';														 --Leave the vsenable off
				end if;
		end if;
	end process;

	process(clkdiv)
	begin
		if(clkdiv = '1' and clkdiv'EVENT and vsenable = '1') then			 --Increment when enabled
			if vc = vlines then															 --Reset when the number of lines is reached
				vc <= "0000000000";
			else vc <= vc + 1;															 --Increment the vertical counter
			end if;
		end if;
	end process;

	process(clkdiv)
	begin
		if (hc(9 downto 7) = "000") then
			hs <= '1';								 --Horizontal Sync Pulse
		else
			hs <= '0';
		end if;

		if (vc(9 downto 1) = "000000000") then
			vs <= '1';						 --Vertical Sync Pulse
		else
			vs <= '0';
		end if;
		
--		if (hc <= 639) then
--			pixel_col <= hc;
--		end if;
--
--		if (vc <= 479) then
--			pixel_row <= vc;
--		end if;

		if (vidon = '1') then
			pixel_col <= hc - 144;
			pixel_row <= vc - 31;
		end if;
		
	end process;

--  	red <= '1' when (hc = "1010101100" and vidon ='1') else '0';			 --Red pixel on at a specific horizontal count
--  	red <= '1' when ((hc(9 downto 6) = "0111") and vc(9 downto 6) = "0100" and vidon ='1') else '0';			 --Dibuja un cuadrado rojo
--		grn <= '1' when (hc = "0100000100" and vidon ='1') else '0';			 --Green pixel on at a specific horizontal count
--  	blu <= '1' when (vc = "0100100001" and vidon ='1') else '0';			 --Blue pixel on at a specific vertical count

  red_out <= '1' when (red_in = '1' and vidon = '1') else '0';			 --Dibuja un cuadrado rojo
	grn_out <= '1' when (grn_in = '1' and vidon = '1') else '0';			 --Green pixel on at a specific horizontal count
 	blu_out <= '1' when (blu_in = '1' and vidon = '1') else '0';			 --Blue pixel on at a specific vertical count
		
	vidon <= '1' when (((hc < hfp) and (hc > hbp)) and ((vc < vfp) and (vc > vbp))) else '0';	--Enable video out when within the porches

end Behavioral;
